package com.controller;

import com.domain.ResponseResult;
import com.domain.Role;
import com.domain.User;
import com.domain.UserVO;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /*
     * 用户分页以及多条件查询
     * */
    @RequestMapping("/findAllUserByPage")
    private ResponseResult findAllUserByPage(@RequestBody UserVO userVO){
        PageInfo<User> pageInfo = userService.findAllUserByPage(userVO);
        return new ResponseResult(true,200,"响应成功",pageInfo);
    }
    /*
     * 用户状态设置
     * */
    @RequestMapping("/updateUserStatus")
    private ResponseResult updateUserStatus(int id,String status){
        userService.updateUserStatus(id, status);
        return new ResponseResult(true,200,"更新状态成功",status);
    }

    /*
    * 用户登陆
    * */
    @RequestMapping("/login")
    public ResponseResult login(User user,HttpServletRequest httpServletRequest) throws Exception {
        User login = userService.login(user);

        if (login != null){
            //保存信息到session中
            HttpSession session = httpServletRequest.getSession();
            String access_token = UUID.randomUUID().toString();
            session.setAttribute("access_token",access_token);
            session.setAttribute("user_id",login.getId());

            Map<String, Object> map = new HashMap<>();
            map.put("access_token",access_token);
            map.put("user_id",login.getId());
            return new ResponseResult(true,200,"登录成功",map);
        }else {
            return new ResponseResult(true,400,"用户名或密码错误",null);
        }
    }
    /*
     * 根据用户id查询相关的角色信息
     * */
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(Integer id){
        List<Role> roles = userService.findUserRelationRoleById(id);
        return new ResponseResult(true,200,"回显角色成功",roles);
    }
    /*
     * 分配角色
     * */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVO userVO){
        userService.userContextRole(userVO);
        return new ResponseResult(true,200,"分配角色成功",null);
    }
    /*
     * 获取用户权限，进行菜单动态展示
     * */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        HttpSession session = request.getSession();
        String access_token = (String)session.getAttribute("access_token");
        if (authorization.equals(access_token)){
            Integer user_id = (Integer)session.getAttribute("user_id");
            ResponseResult responseResult = userService.getUserPermissions(user_id);
            return responseResult;
        }else {
            return new ResponseResult(false,400,"获取权限失败",null);
        }
    }
}
